﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


public class Logger
{
    private static Logger instance;
    private System.IO.StreamWriter fp;
    private String path;

    private Logger()
    {
        path = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/";
    }

    public void Write(Exception exception)
    {
        String content = exception.Message + Environment.NewLine + exception.StackTrace + Environment.NewLine;
        Write(content);
    }

    public void Write(String s)
    {
        try
        {
            lock (instance)
            {
                string fileName = path + DateTime.Now.ToString("yyyyMMdd") + ".log";

                if (System.IO.File.Exists(fileName))
                {
                    fp = System.IO.File.AppendText(fileName);
                }
                else
                {
                    fp = System.IO.File.CreateText(fileName);
                }

                fp.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss ") + s);
            }
        }
        finally
        {
            if (fp != null)
                fp.Close();
        }
    }

    public static Logger Instance
    {
        get
        {
            if (instance == null)
                instance = new Logger();

            return instance;
        }
    }
}

